package com.aizu.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.sword.lang.DateTimeUtil;

import com.aizu.service.CommonService;
import com.isp.utils.JdbcUtils;
import com.isp.web.controller.BaseController;

@Controller
@RequestMapping(value = "/interface")
public class InterfaceController extends BaseController {

	private Logger log = Logger.getLogger(this.getClass());

	public static final String SUCCESS = "success";
	public static final String ERROR_CODE = "errorCode";
	public static final String ERROR_MSG = "errorMsg";

	@Autowired
	private CommonService commonService;

	/**
	 * 房主保存接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveMaster.do")
	@ResponseBody
	public Map<String, Object> saveMaster(HttpServletRequest request, HttpServletResponse response) {
		final String table = "t_rent_main";
		Map<String, Object> json = new HashMap<String, Object>();
		json.put(SUCCESS, 1);

		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String create_user = request.getParameter("createUser");
		String wx_userid = request.getParameter("wxCode");

		if (StringUtils.isBlank(id)) {
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1001");
			json.put(ERROR_MSG, "id不能为空");
			return json;
		}

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", id);

		Map<String, Object> master = null;
		try {
			Map<String, Object> querySqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_QUERY, table, null,
					condition);
			master = this.commonService.findOnly((String) querySqlParam.get(JdbcUtils.SQL),
					(Object[]) querySqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> createSqlParam = null;
		if (null == master) {
			master = new HashMap<String, Object>();
			master.put("id", id);
			master.put("name", name);
			master.put("phone", phone);
			master.put("create_date", new Date());
			master.put("mod_date", new Date());
			master.put("create_user", create_user);
			master.put("wx_userid", wx_userid);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_INSERT, table, master, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			master.put("name", name);
			master.put("phone", phone);
			master.put("mod_date", new Date());
			master.put("create_user", create_user);
			master.put("wx_userid", wx_userid);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_UPDATE, table, master, condition);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			this.commonService.execute((String) createSqlParam.get(JdbcUtils.SQL),
					(Object[]) createSqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1002");
			json.put(ERROR_MSG, "操作失败： " + e.getMessage());
		}

		return json;
	}

	/**
	 * 房客保存接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveGrant.do")
	@ResponseBody
	public Map<String, Object> saveGrant(HttpServletRequest request, HttpServletResponse response) {
		final String table = "t_two_card_grant";
		Map<String, Object> json = new HashMap<String, Object>();
		json.put(SUCCESS, 1);

		String id = request.getParameter("id");
		String rentId = request.getParameter("rentId");
		String nameL = request.getParameter("nameL");
		String sexL = request.getParameter("sexL");
		// String nationL = request.getParameter("nationL");// 名族—(选择)传中文字符
		String bornL = request.getParameter("bornL");
		String idcard = request.getParameter("idnum");
		// String address = request.getParameter("address");// 身份证地址
		String phone = request.getParameter("phone");
		String start_date = request.getParameter("startDate");
		String end_date = request.getParameter("endDate");
		String is_man = request.getParameter("isMan");
		String wx_userid = request.getParameter("wxCode");

		if (StringUtils.isBlank(id)) {
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1001");
			json.put(ERROR_MSG, "id不能为空");
			return json;
		}

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", id);

		Map<String, Object> renter = null;
		try {
			Map<String, Object> querySqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_QUERY, table, null,
					condition);
			renter = this.commonService.findOnly((String) querySqlParam.get(JdbcUtils.SQL),
					(Object[]) querySqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> createSqlParam = null;
		if (null == renter) {
			renter = new HashMap<String, Object>();
			renter.put("id", id);
			renter.put("rentid", rentId);
			renter.put("namel", nameL);
			renter.put("sexl", sexL);
			// renter.put("nationl", nationL);//名族—(选择)传中文字符
			renter.put("bornl", bornL);
			renter.put("idcard", idcard);
			// renter.put("address", address);// 身份证地址
			renter.put("phone", phone);
			renter.put("start_date", start_date);
			renter.put("end_date", end_date);
			renter.put("is_man", is_man);
			renter.put("wx_userid", wx_userid);
			renter.put("create_date", new Date());

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_INSERT, table, renter, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			renter.put("rentid", rentId);
			renter.put("namel", nameL);
			renter.put("sexl", sexL);
			// renter.put("nationl", nationL);//名族—(选择)传中文字符
			renter.put("bornl", bornL);
			renter.put("idcard", idcard);
			// renter.put("address", address);// 身份证地址
			renter.put("phone", phone);
			renter.put("start_date", start_date);
			renter.put("end_date", end_date);
			renter.put("is_man", is_man);
			renter.put("wx_userid", wx_userid);
			if (null == renter.get("create_date"))
				renter.put("create_date", new Date());

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_UPDATE, table, renter, condition);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			this.commonService.execute((String) createSqlParam.get(JdbcUtils.SQL),
					(Object[]) createSqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1002");
			json.put(ERROR_MSG, "操作失败： " + e.getMessage());
		}

		return json;
	}

	/**
	 * 小区保存接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveCommunity.do")
	@ResponseBody
	public Map<String, Object> saveCommunity(HttpServletRequest request, HttpServletResponse response) {
		final String table = "t_community";

		Map<String, Object> json = new HashMap<String, Object>();
		json.put(SUCCESS, 1);

		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String dept_id = request.getParameter("deptId");
		String create_date = StringUtils.defaultIfBlank(request.getParameter("createDate"), DateTimeUtil.currentStr());
		String address = request.getParameter("address");
		String remark = request.getParameter("remark");

		if (StringUtils.isBlank(id)) {
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1001");
			json.put(ERROR_MSG, "id不能为空");
			return json;
		}

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", id);

		Map<String, Object> community = null;
		try {
			Map<String, Object> querySqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_QUERY, table, null,
					condition);
			community = this.commonService.findOnly((String) querySqlParam.get(JdbcUtils.SQL),
					(Object[]) querySqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> createSqlParam = null;
		if (null == community) {
			community = new HashMap<String, Object>();
			community.put("id", id);
			community.put("name", name);
			community.put("longitude", longitude);
			community.put("latitude", latitude);
			community.put("dept_id", dept_id);
			community.put("create_date", create_date);
			community.put("address", address);
			community.put("remark", remark);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_INSERT, table, community, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			community.put("name", name);
			community.put("longitude", longitude);
			community.put("latitude", latitude);
			community.put("dept_id", dept_id);
			community.put("create_date", create_date);
			community.put("address", address);
			community.put("remark", remark);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_UPDATE, table, community, condition);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			this.commonService.execute((String) createSqlParam.get(JdbcUtils.SQL),
					(Object[]) createSqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1002");
			json.put(ERROR_MSG, "操作失败： " + e.getMessage());
		}

		return json;
	}

	/**
	 * 社区保存接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveDepartment.do")
	@ResponseBody
	public Map<String, Object> saveDepartment(HttpServletRequest request, HttpServletResponse response) {
		final String table = "t_department";

		Map<String, Object> json = new HashMap<String, Object>();
		json.put(SUCCESS, 1);

		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String area = request.getParameter("area");
		String create_date = StringUtils.defaultIfBlank(request.getParameter("createDate"), DateTimeUtil.currentStr());
		String street_id = request.getParameter("streetId");
		String user_id = request.getParameter("userId");

		if (StringUtils.isBlank(id)) {
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1001");
			json.put(ERROR_MSG, "id不能为空");
			return json;
		}

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", id);

		Map<String, Object> department = null;
		try {
			Map<String, Object> querySqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_QUERY, table, null,
					condition);
			department = this.commonService.findOnly((String) querySqlParam.get(JdbcUtils.SQL),
					(Object[]) querySqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> createSqlParam = null;
		if (null == department) {
			department = new HashMap<String, Object>();
			department.put("id", id);
			department.put("name", name);
			department.put("province", province);
			department.put("city", city);
			department.put("area", area);
			department.put("create_date", create_date);
			department.put("street_id", street_id);
			department.put("user_id", user_id);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_INSERT, table, department, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			department.put("name", name);
			department.put("province", province);
			department.put("city", city);
			department.put("area", area);
			department.put("create_date", create_date);
			department.put("street_id", street_id);
			department.put("user_id", user_id);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_UPDATE, table, department, condition);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			this.commonService.execute((String) createSqlParam.get(JdbcUtils.SQL),
					(Object[]) createSqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1002");
			json.put(ERROR_MSG, "操作失败： " + e.getMessage());
		}

		return json;
	}

	/**
	 * 街道保存接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveStreet.do")
	@ResponseBody
	public Map<String, Object> saveStreet(HttpServletRequest request, HttpServletResponse response) {
		final String table = "t_street";

		Map<String, Object> json = new HashMap<String, Object>();
		json.put(SUCCESS, 1);

		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String area = request.getParameter("area");
		String create_date = StringUtils.defaultIfBlank(request.getParameter("createDate"), DateTimeUtil.currentStr());

		if (StringUtils.isBlank(id)) {
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1001");
			json.put(ERROR_MSG, "id不能为空");
			return json;
		}

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", id);

		Map<String, Object> street = null;
		try {
			Map<String, Object> querySqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_QUERY, table, null,
					condition);
			street = this.commonService.findOnly((String) querySqlParam.get(JdbcUtils.SQL),
					(Object[]) querySqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> createSqlParam = null;
		if (null == street) {
			street = new HashMap<String, Object>();
			street.put("id", id);
			street.put("name", name);
			street.put("province", province);
			street.put("city", city);
			street.put("area", area);
			street.put("create_date", create_date);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_INSERT, table, street, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			street.put("name", name);
			street.put("province", province);
			street.put("city", city);
			street.put("area", area);
			street.put("create_date", create_date);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_UPDATE, table, street, condition);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			this.commonService.execute((String) createSqlParam.get(JdbcUtils.SQL),
					(Object[]) createSqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1002");
			json.put(ERROR_MSG, "操作失败： " + e.getMessage());
		}

		return json;
	}

	/**
	 * 派出所机构保存接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/savePoliceStation.do")
	@ResponseBody
	public Map<String, Object> savePoliceStation(HttpServletRequest request, HttpServletResponse response) {
		final String table = "t_police_station";

		Map<String, Object> json = new HashMap<String, Object>();
		json.put(SUCCESS, 1);

		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String prent_id = request.getParameter("prentId");
		String street_id = request.getParameter("streetId");
		String create_date = StringUtils.defaultIfBlank(request.getParameter("createDate"), DateTimeUtil.currentStr());
		String province = request.getParameter("province");
		String city = request.getParameter("city");

		if (StringUtils.isBlank(id)) {
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1001");
			json.put(ERROR_MSG, "id不能为空");
			return json;
		}

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", id);

		Map<String, Object> policeStation = null;
		try {
			Map<String, Object> querySqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_QUERY, table, null,
					condition);
			policeStation = this.commonService.findOnly((String) querySqlParam.get(JdbcUtils.SQL),
					(Object[]) querySqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> createSqlParam = null;
		if (null == policeStation) {
			policeStation = new HashMap<String, Object>();
			policeStation.put("id", id);
			policeStation.put("name", name);
			policeStation.put("prent_id", prent_id);
			policeStation.put("street_id", street_id);
			policeStation.put("create_date", create_date);
			policeStation.put("province", province);
			policeStation.put("city", city);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_INSERT, table, policeStation, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			policeStation.put("name", name);
			policeStation.put("prent_id", prent_id);
			policeStation.put("street_id", street_id);
			policeStation.put("create_date", create_date);
			policeStation.put("province", province);
			policeStation.put("city", city);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_UPDATE, table, policeStation, condition);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			this.commonService.execute((String) createSqlParam.get(JdbcUtils.SQL),
					(Object[]) createSqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1002");
			json.put(ERROR_MSG, "操作失败： " + e.getMessage());
		}

		return json;
	}

	/**
	 * 房屋保存接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveRentRoom.do")
	@ResponseBody
	public Map<String, Object> saveRentRoom(HttpServletRequest request, HttpServletResponse response) {
		final String table = "t_rent_room";

		Map<String, Object> json = new HashMap<String, Object>();
		json.put(SUCCESS, 1);

		String id = request.getParameter("id");
		String community_id = request.getParameter("communityId");
		String building = request.getParameter("building");
		String unit = request.getParameter("unit");
		String room_num = request.getParameter("roomNum");
		String room_type = request.getParameter("roomType");
		String ren_type = request.getParameter("renType");
		String main_id = request.getParameter("mainId");
		String create_date = StringUtils.defaultIfBlank(request.getParameter("createDate"), DateTimeUtil.currentStr());
		String is_rent = request.getParameter("isRent");
		String address = request.getParameter("address");
		String dept_id = request.getParameter("deptId");
		String read_head_id = request.getParameter("readHeadId");
		String equipment_id = request.getParameter("equipmentId");

		if (StringUtils.isBlank(id)) {
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1001");
			json.put(ERROR_MSG, "id不能为空");
			return json;
		}

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("id", id);

		Map<String, Object> rentRoom = null;
		try {
			Map<String, Object> querySqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_QUERY, table, null,
					condition);
			rentRoom = this.commonService.findOnly((String) querySqlParam.get(JdbcUtils.SQL),
					(Object[]) querySqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> createSqlParam = null;
		if (null == rentRoom) {
			rentRoom = new HashMap<String, Object>();
			rentRoom.put("id", id);
			rentRoom.put("community_id", community_id);
			rentRoom.put("building", building);
			rentRoom.put("unit", unit);
			rentRoom.put("room_num", room_num);
			rentRoom.put("room_type", room_type);
			rentRoom.put("ren_type", ren_type);
			rentRoom.put("main_id", main_id);
			rentRoom.put("create_date", create_date);
			rentRoom.put("is_rent", is_rent);
			rentRoom.put("address", address);
			rentRoom.put("dept_id", dept_id);
			rentRoom.put("read_head_id", read_head_id);
			rentRoom.put("equipment_id", equipment_id);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_INSERT, table, rentRoom, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			rentRoom.put("community_id", community_id);
			rentRoom.put("building", building);
			rentRoom.put("unit", unit);
			rentRoom.put("room_num", room_num);
			rentRoom.put("room_type", room_type);
			rentRoom.put("ren_type", ren_type);
			rentRoom.put("main_id", main_id);
			rentRoom.put("create_date", create_date);
			rentRoom.put("is_rent", is_rent);
			rentRoom.put("address", address);
			rentRoom.put("dept_id", dept_id);
			rentRoom.put("read_head_id", read_head_id);
			rentRoom.put("equipment_id", equipment_id);

			try {
				createSqlParam = JdbcUtils.createSqlParam(JdbcUtils.SQLTYPE_UPDATE, table, rentRoom, condition);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			this.commonService.execute((String) createSqlParam.get(JdbcUtils.SQL),
					(Object[]) createSqlParam.get(JdbcUtils.PARAM));
		} catch (Exception e) {
			e.printStackTrace();
			json.put(SUCCESS, 0);
			json.put(ERROR_CODE, "1002");
			json.put(ERROR_MSG, "操作失败： " + e.getMessage());
		}

		return json;
	}

}
